*This file replicates regression results for Investment and Employment outcomes - Select Environment firms classified as Defense firms.
*The code used in this file is adapted from Baker, Bloom, and Davis (2016) to generate results related to environmental policy uncertainty.

cap cd "panel_regressions"
clear all
use data/all_firm_data_updated
set more off

so gvkey year quarter
merge 1:1 gvkey year quarter using data/tobin_q_data, keepus(tobin_q) 
ren tobin_q tobin
drop if _m==2  
drop _m
so gvkey year quarter
merge 1:1 gvkey year quarter using data/cashflow_data, keepus(cashflow)
tab _m
drop if _m==2
drop _m

merge m:1 year quarter using data/modern_epu_quarterly 

foreach var in E P U EP EU PU EPU {
	gen lm_`var'=log(m_`var')
}
drop if _merge==2
drop _merge
merge m:1 year quarter using data/categories_yq 
gen yq=year+(quarter-1)/4
gen lepu=log(m_EPU)
gen leu=log(m_EU)

********************Updating/Fixing VIX
cap drop _merge
ren vix oldvix
so yq
merge m:1 yq using data/vix.dta,update
replace lvix=log(vix)
drop _m
merge m:1 yq using data/vxo_quarterly.dta,update
drop _m
replace vix=vxo if vix==.&vxo~=.
replace lvix=log(vix)

***************Add in Steve's intensity measure
so gvkey
merg m:1 gvkey using data/gvkey
drop if _m==2
drop _m
so cusip
merge m:1 cusip using data/steve10k_cusip
winsor2 jp,replace 

drop if _m==2
drop _m
ren jp jp 

merge m:1 year quarter using data/04_Env_Policy_indices_qtr

************************************************************************************************************************
***Here we make all of the necessary interactions for each intensity measure
************************************************************************************************************************
tsset ticker_code period_code

foreach var of varlist h_firm_sic_intens  {
	foreach var2 of varlist m_* lm_* vix lepu lvix fed fed_fore leu {
		qui gen `var2'_X_`var' = `var2'*`var'
		qui gen l1`var2'_X_`var' = l1.`var2'_X_`var'
		qui gen dl1`var2'_X_`var' = d.l1`var2'_X_`var'
	}
}

foreach var of varlist firm_intens sic_intens firm_sic_intens belo_intens jp {
	foreach var2 of varlist lepu fed {
		qui gen `var2'_X_`var' = `var2'*`var'
		qui gen l1`var2'_X_`var' = l1.`var2'_X_`var'
		qui gen dl1`var2'_X_`var' = d.l1`var2'_X_`var'
	}
}
gen lagppent=l.ppent

gen l1lepu=l1.lepu
gen dl1lepu=d.l1lepu
gen l1fed=l1.fed
gen dl1fed=d.l1fed
gen SIC2=int(sic4/100)

* Sectoral policy indices
gen env_firms=1 if inlist(sic3, 104, 109, 122, 131, 138, 241, 242, 491, 492) | (inlist(SIC2, 1, 2, 37) & sic3!=372 & sic3!=376 & sic3!=379)
replace env_firms=0 if env_firms==.
foreach var in EPU_Factiva EP_Factiva EPU_NB EP_NB EPU_NB_local EP_NB_local {
replace `var'=0 if env_firms==0
}
*
drop _m
merge m:1 year quarter using data/A_DP_FP_HP_Quarter
drop _m

*
gen def_firms = inlist(sic3, 348, 372, 376, 379, 381, 871)
foreach var in defense dp dpu {
replace `var'=0 if def_firms==0
}

gen health_firms=(sic3>=800&sic3<=809)
foreach var in health hp hpu{
replace `var'=0 if health_firms==0
}

gen fin_firms= (sic3>=600&sic3<=679) 
foreach var in fin_regulation fp fpu{
replace `var'=0 if fin_firms==0
}

tsset ticker_code period_code

foreach var in EPU_NB EP_NB defense health fin_regulation dp dpu hp hpu fp fpu{
gen l`var'=log(`var')
gen dl`var'=d.l`var'
gen dl`var'_miss=(dl`var'==.)
replace dl`var'=0 if dl`var'_miss==1
}

********************************************************************************
label var iqr_fed_gov_forecast "Fed Exp. IQR"

***Compress
drop nom_fed_exp nom_gdp  vxo lvxo iqr_fed_gov_forecast ticker
egen avg_emp = mean(emp), by(ticker_code)
replace avg_emp=round(1000*avg_emp)

replace avg_emp=1 if avg_emp==0
egen mavg_emp=mean(avg_emp)
replace avg_emp=round(mavg_emp) if avg_emp==.
egen min_year = min(year), by(ticker_code )
gen age = year-min_year
drop *long obligatedamount atq xrd iq lrevtq firm_cap_intens firm_rd_intens sic2 
compress

gen inv_rate = capxy / l.ppentq

gen sizeemp=emp
replace sizeemp=round(1000*sizeemp)

save data/itemp,replace
cap log close
cap log using invemp,replace t

******** Select Environment firms classified as Defense firms (Table A2)
reghdfe invest_ppe_rate dlEPU_NB dlEP_NB dldpu dldp dlhpu dlhp dlfpu dlfp dl1lepu_X_h_firm_sic_intens dl1fed_X_h_firm_sic_intens,  ab(ticker_code period_code) vce(cl ticker)

************************************************************************

******************************************
***************EMPLOYMENT
***Collapse to yearly data
clear all
use data/annual_firm_data 
gen period_code=year
tsset ticker_code period_code
gen one=1

foreach var of varlist one firm_intens sic_intens firm_sic_intens health_sic_intens health_firm_intens belo_intens h_firm_sic_intens {
	foreach var2 of varlist lepu leu lvix fed  fed_fore {
		qui gen `var2'_X_`var' = `var2'*`var'
		qui gen d`var2'_X_`var' = d.`var2'_X_`var'
	}
}

tsset 
gen demp = (emp - l.emp)/(0.5*emp+0.5*l.emp)
gen drev = (revtq - l.revtq)/(0.5*revtq+0.5*l.revtq)
replace drev = . if drev>=2 |drev<=-2

gen dlepu = d.lepu
gen dh_firm_sic_intens =d.h_firm_sic_intens
gen dfed = d.fed

gen SIC2=int(sic4/100)

merge m:1 year using data/04_Env_Policy_indices_annual
tsset

* Sectoral policy indices
gen env_firms=1 if inlist(sic3, 104, 109, 122, 131, 138, 241, 242, 491, 492) | (inlist(SIC2, 1, 2, 37) & sic3!=372 & sic3!=376 & sic3!=379)
replace env_firms=0 if env_firms==.
foreach var in EPU_Factiva EP_Factiva EPU_NB EP_NB EPU_NB_local EP_NB_local {
replace `var'=0 if env_firms==0
}
*
drop _m
merge m:1 year using data/A_DP_FP_HP_Year
drop _m

*
gen def_firms = inlist(sic3, 348, 372, 376, 379, 381, 871)
foreach var in defense dp dpu {
replace `var'=0 if def_firms==0
}

gen health_firms=(sic3>=800&sic3<=809)
foreach var in health hp hpu{
replace `var'=0 if health_firms==0
}

gen fin_firms= (sic3>=600&sic3<=679) 
foreach var in fin_regulation fp fpu{
replace `var'=0 if fin_firms==0
}

tsset ticker_code period_code

foreach var in EPU_NB EP_NB dp dpu hp hpu fp fpu{
gen l`var'=log(`var')
gen dl`var'=d.l`var'
gen dl`var'_miss=(dl`var'==.)
replace dl`var'=0 if dl`var'_miss==1
}
egen noj=count(demp),by(ticker_code)
keep if noj>1
save data/etemp,replace

******** Select Environment firms classified as Defense firms (Table A2)
reghdfe demp dlEPU_NB dlEP_NB dldpu dldp dlhpu dlhp dlfpu dlfp dlepu_X_h_firm_sic_intens dfed_X_h_firm_sic_intens, ab(ticker_code period_code) vce(cl ticker)
log close

****************************************************************************************
